package com.zh.autoconfigure;

import com.zh.config.SaTokenConfigure;
import com.zh.config.StpInterfaceImpl;
import com.zh.interceptor.TokenInterceptor;
import com.zh.properties.SatokenProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.client.RestTemplate;

// 认证satoken

// 标记配置类
@Configuration
@EnableConfigurationProperties(SatokenProperties.class)
// 导入配置
public class SaTokenAutoConfiguration {

    //SaToken 拦截器配置
    @Bean
    public SaTokenConfigure saTokenConfigure(SatokenProperties satokenProperties) {
        return  new SaTokenConfigure(satokenProperties);
    }


    //加载权限的接口
    @Bean
    public StpInterfaceImpl stpInterface (RedisTemplate<Object,Object> redisTemplate) {
        return  new StpInterfaceImpl(redisTemplate);
    }

    /**
     *  token转发的拦截器
     * @return
     */
    @Bean
    public TokenInterceptor tokenInterceptor(){
        return new TokenInterceptor();
    }

    /**
     * 密码编码器(用于给密码进行加密，采用了SHA-256+随机盐+密钥对密码进行加密)
     */
    @Bean
    public PasswordEncoder passwordEncoder (){
        return new BCryptPasswordEncoder();
    }

}
